package com.flyinzhen.customer.service;

import com.flyinzhen.core.constants.ResponseStatus;
import com.flyinzhen.core.dto.ResponseDto;
import com.flyinzhen.core.service.RedisService;
import com.flyinzhen.customer.constants.AccountType;
import com.flyinzhen.customer.constants.PasswordType;
import com.flyinzhen.customer.constants.RedisCacheType;
import com.flyinzhen.customer.dto.input.AuthLoginInput;

import com.flyinzhen.customer.dto.input.AuthUpdateInput;
import com.flyinzhen.customer.entity.CustRegistInfo;
import com.flyinzhen.customer.repository.CustRegistRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.NoSuchElementException;

import static com.flyinzhen.customer.utils.ApplicationUtils.cacheKey;

/**
 * 客户验证实现类
 *
 * @author yinzhen
 * @date 2019-05-01
 */
@Slf4j
@Service
public class AuthorityService {

	@Resource
	private RedisService redisService;

	@Resource
	private CustRegistRepository registRepository;

	public ResponseDto login(AuthLoginInput inputDto) {
		// TODO facebook登录
		// 如果密码类型是验证码,校验验证码是否正确;如果密码类型是授权,校验是否facebook登录;其它校验密码是否匹配
		CustRegistInfo registInfo = registRepository.findTopByOrgCodeAndAccountNo(inputDto.getOrgCode(), inputDto.getAccountNo());
		if (PasswordType.CAPTCHA.name().equals(inputDto.getPasswordType())) {
			String captchaKey = cacheKey(RedisCacheType.CAPTCHA_SMS.name(), inputDto.getAccountNo());
			if (!inputDto.getPassword().equals(redisService.get(captchaKey))) {
				return ResponseDto.status(ResponseStatus.CAPTCHA_INVALID);
			}
		} else if (PasswordType.LICENSE.name().equals(inputDto.getPasswordType())) {
			if (!AccountType.FACEBOOK.name().equals(inputDto.getAccountType())) {
				return ResponseDto.status(ResponseStatus.PARAM_INVALID);
			}
		} else if (PasswordType.ACCOUNT_PWD.name().equals(inputDto.getPasswordType())) {
			if (registInfo == null || !inputDto.getPassword().equals(registInfo.getAccountPwd())) {
				return ResponseDto.status(ResponseStatus.PASSWORD_INVALID);
			}
		} else if (PasswordType.GESTURE_PWD.name().equals(inputDto.getPasswordType())) {
			if (registInfo == null || !inputDto.getPassword().equals(registInfo.getGesturePwd())) {
				return ResponseDto.status(ResponseStatus.PASSWORD_INVALID);
			}
		} else {
			return ResponseDto.status(ResponseStatus.PARAM_INVALID);
		}

		// 查询账号信息，如果账号不存在则创建账号
		boolean isRegisted = (registInfo != null);
		if (!isRegisted) {
			registInfo = createCustInfo(inputDto);
		}

		return ResponseDto.ok(registInfo);
	}

	@Transactional
	public ResponseDto update(AuthUpdateInput inputDto) {
		// 获取注册信息
		Integer userId = inputDto.getUserId();
		CustRegistInfo registInfo = registRepository.findById(userId).orElseThrow(NoSuchElementException::new);

		// 更新数据
		BeanUtils.copyProperties(inputDto, registInfo);
		if (PasswordType.GESTURE_PWD.name().equals(inputDto.getPasswordType())) {
			registInfo.setGesturePwd(inputDto.getPassword());
		} else if (PasswordType.ACCOUNT_PWD.name().equals(inputDto.getPasswordType())) {
			registInfo.setAccountPwd(inputDto.getPassword());
		}
		registRepository.save(registInfo);
		return new ResponseDto();
	}

	private CustRegistInfo createCustInfo(AuthLoginInput inputDto) {
		CustRegistInfo registInfo = new CustRegistInfo();

		BeanUtils.copyProperties(inputDto, registInfo);
		if (AccountType.PHONE.name().equals(inputDto.getAccountType())) {
			registInfo.setPhone(inputDto.getAccountNo());
		} else if (AccountType.FACEBOOK.name().equals(inputDto.getAccountType())) {
			registInfo.setFacebook(inputDto.getAccountNo());
		}
		if (PasswordType.ACCOUNT_PWD.name().equals(inputDto.getPasswordType())) {
			registInfo.setAccountPwd(inputDto.getPassword());
		} else if (PasswordType.GESTURE_PWD.name().equals(inputDto.getPasswordType())) {
			registInfo.setGesturePwd(inputDto.getPassword());
		}

		registRepository.save(registInfo);
		return registInfo;
	}
}
